package com.sky.controller.admin;

import com.sky.constant.JwtClaimsConstant;
import com.sky.dto.EmployeeDTO;
import com.sky.dto.EmployeeLoginDTO;
import com.sky.dto.EmployeePageQueryDTO;
import com.sky.entity.Employee;
import com.sky.properties.JwtProperties;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.EmployeeService;
import com.sky.utils.JwtUtil;
import com.sky.vo.EmployeeLoginVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 员工管理
 */
@RestController//注解@RestController用于标识类为控制器，将返回值直接转换为JSON格式
@RequestMapping("/admin/employee")
@Slf4j
@Api(tags = "员工相关接口")//接口描述注解，用于接口文档
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;
    @Autowired
    private JwtProperties jwtProperties;

    /**
     * 登录
     *
     * @param employeeLoginDTO
     * @return
     */
    @PostMapping("/login")//员工登录
    @ApiOperation(value = "员工登录")//功能描述注解，用于接口文档
    public Result<EmployeeLoginVO> login(@RequestBody EmployeeLoginDTO employeeLoginDTO) {
        log.info("员工登录：{}", employeeLoginDTO);

        Employee employee = employeeService.login(employeeLoginDTO);

        //登录成功后，生成jwt令牌
        Map<String, Object> claims = new HashMap<>();
        claims.put(JwtClaimsConstant.EMP_ID, employee.getId());
        String token = JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        EmployeeLoginVO employeeLoginVO = EmployeeLoginVO.builder()
                .id(employee.getId())
                .userName(employee.getUsername())
                .name(employee.getName())
                .token(token)
                .build();

        return Result.success(employeeLoginVO);//程序结束
    }

    /**
     * 退出
     *
     * @return
     */
    @PostMapping("/logout")
    @ApiOperation("员工退出")//value已省略
    public Result<String> logout() {
        return Result.success();
    }

    /**
     * 新增员工
     * @param employeeDTO
     * @return
     */
    @PostMapping//添加这个注释是因为前端提交的数据格式是JSON，所以参数需要再使用@RequestBody注解
    @ApiOperation("新增员工")//value已省略,功能描述注解，用于接口文档
    public Result save(@RequestBody EmployeeDTO employeeDTO){
        log.info("新增员工：{}",employeeDTO);
        System.out.println("当前现成的ID为：" + Thread.currentThread().getId());
        employeeService.save(employeeDTO);
        return Result.success();
    }

    /**
     * 分页查询员工
     * @param employeePageQueryDTO
     * @return
     */
    @GetMapping("/page")//分页查询员工
    @ApiOperation("员工分页查询")//value已省略,功能描述注解，用于接口文档
    //返回格式为Query,系统会自动将Query转换为JSON格式，所以不需要再使用@ResponseBody注解
    public Result<PageResult> page(EmployeePageQueryDTO employeePageQueryDTO){
        log.info("分页查询员工：参数为{}",employeePageQueryDTO);
        PageResult pageResult = employeeService.pageQuery(employeePageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 启用禁用员工账号
     * @param status
     * @param id
     * @return
     */
    @PostMapping("/status/{status}")
    @ApiOperation("启用禁用员工账号")
    //由于status参数为路径参数，所以需要添加@PathVariable注解,
    // id参数为路径参数因为前端提交的数据格式是JSON，所以参数需要再使用@RequestBody注解，其原理为将JSON数据转换为Java对象
    //本方法非查询方法，Result不加泛型
    public Result startOrStop(@PathVariable Integer status,Long id) {
        log.info("启用禁用员工账号：id={},status={}", id, status);
        employeeService.startOrStop(status,id);
        return Result.success();
        }

    /**
     * 根据员工ID查询员工信息
      * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据员工ID查询员工信息")
    public Result<Employee> getByID(@PathVariable Long id) {
        log.info("查询员工：id={}", id);
        Employee employee = employeeService.getById(id);
        return Result.success(employee);
    }

    @PutMapping
    @ApiOperation("更新员工信息")
    public Result update(@RequestBody EmployeeDTO employeeDTO) {
        log.info("更新员工信息：{}", employeeDTO);
        employeeService.update(employeeDTO);
        return Result.success();
    }
}
